package com.hxzy.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.shop.common.dto.merchant.AnnouncementConvert;
import com.hxzy.shop.common.dto.merchant.req.AnnouncementMerchantListReq;
import com.hxzy.shop.common.dto.merchant.resp.AnnouncementDetailResp;
import com.hxzy.shop.constant.AuthConstant;
import com.hxzy.shop.dto.UserDto;
import com.hxzy.shop.dto.req.AnnouncementIdReq;
import com.hxzy.shop.dto.resp.AnnouncementClassificationListResp;
import com.hxzy.shop.dto.resp.AnnouncementMerchantDetailResp;
import com.hxzy.shop.entity.Announcement;
import com.hxzy.shop.entity.AnnouncementAccountLook;
import com.hxzy.shop.entity.AnnouncementSendRecord;
import com.hxzy.shop.mapper.AnnouncementMapper;
import com.hxzy.shop.service.AnnouncementAccountLookService;
import com.hxzy.shop.service.AnnouncementSendRecordService;
import com.hxzy.shop.service.AnnouncementService;
import com.hxzy.shop.utils.WebUtil;
import com.hxzy.shop.vo.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述
 *
 * @author tonneyyy
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;

    /**
     * 公告发送记录表
     */
    @Autowired
    private AnnouncementSendRecordService announcementSendRecordService;


    /**
     * 账户公告查阅(AnnouncementAccountLook)表服务实现类
     */
    @Autowired
    private AnnouncementAccountLookService announcementAccountLookService;

//    /**
//     * 根据id获取当前门店公告
//     * @param announcementId
//     * @return
//     */
//    @Override
//    public AnnouncementDetailResp announcementLook(Long announcementId) {
//        Announcement announcement = announcementMapper.getAnnouncement(announcementId);
//        if (ObjectUtil.isEmpty(announcement)) {
//            throw new ApiException(500, "该公告已过时，或已被删除");
//        }
//
//        /**
//         * 获取当前门店id
//         * */
//        HttpServletRequest request = WebUtil.getRequest();
//        String merchantId = request.getHeader(AuthConstant.MERCHANT_ID);
//        //从请求头中获取当前用户
//        String userStr = request.getHeader(AuthConstant.USER_TOKEN_HEADER);
//        UserDto userDto = JSONUtil.toBean(userStr, UserDto.class);
//
//        AnnouncementSendRecord one = announcementSendRecordService.getOne(new LambdaQueryWrapper<AnnouncementSendRecord>().eq(AnnouncementSendRecord::getAnnouncementId, announcementId)
//                .eq(AnnouncementSendRecord::getMerchantId, merchantId));
//
//        //Announcement 转换为  AnnouncementDetailResp
//         AnnouncementDetailResp announcementDetailResp= AnnouncementConvert.INSTANCE.convert(announcement);
//         one.setIsLook(1);
//        this.announcementSendRecordService.updateById(one);
//
//        //登录弹出
//        if(announcement.getRule()==1){
//            AnnouncementAccountLook announcementAccountLook = announcementAccountLookService.getOne(new LambdaQueryWrapper<AnnouncementAccountLook>()
//                    .eq(AnnouncementAccountLook::getAnnouncementId, announcementId)
//                    .eq(AnnouncementAccountLook::getAccountId, userDto.getId())
//                    .eq(AnnouncementAccountLook::getMerchantId, merchantId));
//            if(ObjectUtil.isNotEmpty(announcementAccountLook) && announcementAccountLook.getIsLook() == 0){
//               announcementAccountLook.setIsLook(1);
//               announcementAccountLookService.updateById(announcementAccountLook);
//            }
//        }
//
//        return announcementDetailResp;
//    }

    @Override
    public PageInfo<AnnouncementMerchantDetailResp> getAnnouncementMerchant(AnnouncementMerchantListReq req) {
        HttpServletRequest request = WebUtil.getRequest();
        String merchantId = request.getHeader(AuthConstant.MERCHANT_ID);
        PageInfo<AnnouncementMerchantDetailResp> objectPageInfo = PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(()
                -> announcementMapper.getAnnouncementMerchantList(req, merchantId));
        return objectPageInfo;
    }


    @Override
    public Integer getAnnouncementNotLook() {
        AnnouncementMerchantListReq req = new AnnouncementMerchantListReq();
        req.setPageNum(1);
        req.setPageSize(10);
        req.setIsLook(0);
        PageInfo<AnnouncementMerchantDetailResp> announcementMerchant = this.getAnnouncementMerchant(req);

        return Integer.parseInt(String.valueOf(announcementMerchant.getTotal()));
    }

    @Override
    public List<AnnouncementClassificationListResp> getAnnouncementClassification(Integer isLook) {

        HttpServletRequest request = WebUtil.getRequest();
        String merchantId = request.getHeader(AuthConstant.MERCHANT_ID);
        //全部消息分类 单独查询
        AnnouncementMerchantListReq req = new AnnouncementMerchantListReq();
        req.setPageNum(1);
        req.setPageSize(10);
        req.setIsLook(0);
        PageInfo<AnnouncementMerchantDetailResp> announcementMerchant = this.getAnnouncementMerchant(req);
        AnnouncementClassificationListResp announcementClassificationListResp = new AnnouncementClassificationListResp();
        announcementClassificationListResp.setAnnouncementClassificationId(0l);
        announcementClassificationListResp.setClassificationName("全部消息");
        announcementClassificationListResp.setAnnouncementNum(Integer.parseInt(String.valueOf(announcementMerchant.getTotal())));
        List<AnnouncementClassificationListResp> announcementClassificationListRespList = new ArrayList<>();
//        if(isLook==1){
//            announcementClassificationListRespList = announcementSendRecordService.getAnnouncementClassificationListRespList(merchantId,null);
//            announcementClassificationListRespList.add(0, announcementClassificationListResp);
//        }else{
        announcementClassificationListRespList = announcementSendRecordService.getAnnouncementClassificationListRespList(merchantId, null);

        announcementClassificationListRespList = announcementClassificationListRespList.stream().map(aAnnouncementClassificationListResp -> {
            Long announcementClassificationId = aAnnouncementClassificationListResp.getAnnouncementClassificationId();
            Integer announcementSendNum = announcementSendRecordService.getAnnouncementSendNum(merchantId, announcementClassificationId, 0);
            aAnnouncementClassificationListResp.setAnnouncementNum(announcementSendNum);
            return aAnnouncementClassificationListResp;
        }).collect(Collectors.toList());
        announcementClassificationListRespList.add(0, announcementClassificationListResp);
//        }

        return announcementClassificationListRespList;
    }


    @Override
    public AnnouncementDetailResp announcementLook(AnnouncementIdReq req) {
        /**
         * 获取当前门店id
         * */
        HttpServletRequest request = WebUtil.getRequest();
        String merchantId = request.getHeader(AuthConstant.MERCHANT_ID);
        //从请求头中获取当前用户
        String userStr = request.getHeader(AuthConstant.USER_TOKEN_HEADER);
        UserDto userDto = JSONUtil.toBean(userStr, UserDto.class);
        Announcement announcement = announcementMapper.getAnnouncement(req);
        if (ObjectUtil.isEmpty(announcement)) {
            throw new ApiException(500, "该公告已过时，或已被删除");
        }


        AnnouncementSendRecord one = announcementSendRecordService.getOne(new LambdaQueryWrapper<AnnouncementSendRecord>().eq(AnnouncementSendRecord::getAnnouncementId, req.getAnnouncementId())
                .eq(AnnouncementSendRecord::getMerchantId, merchantId));
        AnnouncementDetailResp announcementDetailResp = AnnouncementConvert.INSTANCE.toAnnouncementDetailResp(announcement);
        one.setIsLook(1);
        announcementSendRecordService.updateById(one);
        if (announcement.getRule() == 1) {
            //证明是登录查看
            AnnouncementAccountLook announcementAccountLook = announcementAccountLookService.getOne(new LambdaQueryWrapper<AnnouncementAccountLook>()
                    .eq(AnnouncementAccountLook::getAnnouncementId, req.getAnnouncementId())
                    .eq(AnnouncementAccountLook::getAccountId, userDto.getId())
                    .eq(AnnouncementAccountLook::getMerchantId, merchantId));
            if (ObjectUtil.isNotEmpty(announcementAccountLook) && announcementAccountLook.getIsLook() == 0) {
                announcementAccountLook.setIsLook(1);
                announcementAccountLookService.updateById(announcementAccountLook);
            }
        }

        return announcementDetailResp;
    }
}
